var a = null, t = getApp();

Component({
    options: {
        addGlobalClass: !0
    },
    properties: {
        openUpload: {
            value: !1,
            type: Boolean
        },
        audioSrc: {
            value: "",
            type: String
        },
        drag: {
            value: !0,
            type: Boolean
        }
    },
    observers: {
        audioSrc: function(a, t) {
            this.setData({
                currentTime: 0,
                playFlag: !1,
                playingFlag: !1
            }), this.createInnerAudio();
        }
    },
    data: {
        duration: 0,
        currentTime: 0,
        playFlag: !1,
        playingFlag: !1,
        draging: !1
    },
    ready: function() {
        this.createInnerAudio();
    },
    lifetimes: {
        detached: function() {
            a.destroy();
        }
    },
    methods: {
        getAudioFile: function() {
            var a = this;
            wx.chooseMessageFile({
                count: 1,
                type: "file",
                extension: [ ".mp3", "mp3" ],
                success: function(t) {
                    a.uploadAudioM(t.tempFiles[0].path);
                },
                fail: function(a) {}
            });
        },
        createInnerAudio: function() {
            var t = this;
            null != a && a.destroy(), "" != this.data.audioSrc && ((a = wx.createInnerAudioContext()).src = this.data.audioSrc, 
            a.onCanplay(function(n) {
                a.duration, setTimeout(function() {
                    var n = parseInt(a.duration);
                    t.setData({
                        duration: n,
                        playFlag: !0,
                        draging: !1
                    });
                }, 100);
            }), a.onTimeUpdate(function(n) {
                if (!t.data.draging) {
                    var e = parseInt(a.currentTime), i = parseInt(a.duration);
                    if (e != t.data.currentTime) {
                        var o = {
                            currentTime: e
                        };
                        i != t.data.duration && (o.duration = i), t.setData(o);
                    }
                }
            }), a.onPlay(function() {
                t.setData({
                    draging: !1
                }), t.setData({
                    playingFlag: !0
                });
            }), a.onPause(function() {
                t.setData({
                    playingFlag: !1
                });
            }), wx.onAudioInterruptionBegin(function() {
                t.setData({
                    playingFlag: !1
                });
            }), wx.onAudioInterruptionEnd(function() {
                t.setData({
                    playingFlag: !1
                });
            }), a.onEnded(function() {
                t.setData({
                    playingFlag: !1
                });
            }), a.onError(function() {}));
        },
        handPlayAudio: function() {
            this.data.playFlag && (this.data.playingFlag ? (this.setData({
                playingFlag: !1
            }), a.pause()) : (this.setData({
                playingFlag: !0
            }), a.play()));
        },
        uploadAudioM: function(a) {
            var n = this, e = t.getNetAddresss("upload.uploadPic");
            e += "&upload_type=audio", wx.showLoading({
                title: "上传中"
            }), wx.uploadFile({
                url: e,
                filePath: a,
                name: "file",
                formData: {},
                success: function(a) {
                    var e = JSON.parse(a.data);
                    n.triggerEvent("successUp", {
                        res: e
                    }), 1 != e.result && t.tips(e.msg);
                },
                complete: function() {
                    wx.hideLoading();
                }
            });
        },
        startDrag: function() {
            this.setData({
                draging: !0
            });
        },
        endDrag: function(t) {
            a.seek(t);
        }
    }
});